<?php
require_once "db.php";
session_start();

$cinema = db_escape($_POST['oneprogram']);
$date = db_escape($_POST['oneprogramdate']);
$movie = $_SESSION['movie'];

function my_sort($a,$b)
{
	if ($a==$b) return 0;
	  return ($a<$b)?-1:1;
}

if ($cinema==="choose_cinema") {
	echo "<div class='one-program-hours'><span id='noproj_span'>Choose cinema.</span></div>";
	exit();
}
$take_cinemaid = "SELECT idcinema FROM cinema WHERE cinema_name = '$cinema'";
$get_cinemaid = db_query($take_cinemaid);
while ($cinemaid = mysqli_fetch_assoc($get_cinemaid)) {
	$cinema_id = $cinemaid['idcinema'];
}
$proj_query_text = "SELECT idprojection, proj_begin FROM projection WHERE proj_date='$date' AND movies_idmovies='$movie' AND cinema_idcinema='$cinema_id'";
$proj_query = db_query($proj_query_text);
if (mysqli_num_rows($proj_query)===0) {
	$program_content = "<div class='one-program-hours'><span id='noproj_span'>There are no projections on that day.</span></div>";
	echo $program_content;
}
else{
	$date_id = array();
	$date_keys = array();
	while ($proj_row = mysqli_fetch_assoc($proj_query)){
		$date_id[strtotime($proj_row['proj_begin'])]=$proj_row['idprojection'];
		$date_keys[] = strtotime($proj_row['proj_begin']);
	}
	usort($date_keys, 'my_sort');
	$program_content = "<div class='one-program-hours'>";
	foreach ($date_keys as $value) {
		$program_content .= "<div class='hour_begin'><a href='#' id='".$date_id[$value]."'>".date('H:i', $value)."</a></div>";
	}
	$program_content .= "</div>";
	echo $program_content;
}


